package zj.com.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.ViewPagerAdapter;
import zj.com.zj.R;

/**
 * Created by xi on 16/5/31.
 */
public class GuideActivity extends  BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {
    private ViewPager vp;
    private ViewPagerAdapter vpAdapter;
    private List<View> views;

    public ImageView enter_iv;

    //引导图片资源
    private static final int[] pics = { R.drawable.a,
            R.drawable.b, R.drawable.c};
    private int currentIndex;
    SharedPreferences mySharedPreferences;
    SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
         mySharedPreferences= getSharedPreferences("zj",
                Activity.MODE_PRIVATE);
       editor = mySharedPreferences.edit();
        views = new ArrayList<View>();
        enter_iv = (ImageView) findViewById(R.id.enter_iv);
        enter_iv.setOnClickListener(this);
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        //初始化引导图片列表
        for(int i=0; i<pics.length; i++) {
            ImageView iv = new ImageView(this);
            iv.setLayoutParams(mParams);
            iv.setBackgroundResource(pics[i]);
            views.add(iv);
        }
        vp = (ViewPager) findViewById(R.id.viewpager);
        //初始化Adapter
        vpAdapter = new ViewPagerAdapter(views);
        vp.setAdapter(vpAdapter);
        //绑定回调
        vp.setOnPageChangeListener(this);
    }

    public Intent intent ;
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.enter_iv:
                editor.putInt("first",1);
                editor.commit();
                intent = new Intent(this,LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }
    /**
     *设置当前的引导页
     */
    private void setCurView(int position)
    {
        if (position < 0 || position >= pics.length) {
            return;
        }

        vp.setCurrentItem(position);
    }

    @Override
    public void onPageSelected(int position) {

        if(position == 2){

            enter_iv.setVisibility(View.VISIBLE);
        }else{
            enter_iv.setVisibility(View.GONE);

        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
